#include "StdAfx.h"
#include "DllWrapper.h"
#include "faxbackupuiexport.h"

PFNCreateAppConfigObject pCreateAppConfigObject;
PFNInitAppRegisterInfo pInitAppRegisterInfo;
PFNCreateRegisterAppInfoObject pCreateRegisterAppInfoObject;
PFNGetAppRegisterInfoByExePath pGetAppRegisterInfoByExePath;
PFNGetAppRegisterInfoByinstallPath pGetAppRegisterInfoByinstallPath;
PFNGetConfigBySerialNum pGetConfigBySerialNum;
PFNIsOtherInstanceRunning pIsOtherInstanceRunning;
PFNGetTaxUsbManager pGetTaxUsbManager;
PFNGetPassword pGetPassword;
PFNShowProgress pShowProgress;
PFNHideProgress pHideProgress;

DllWrapper g_dllWrapper;

DllWrapper::DllWrapper(void)
{
	m_isInited = FALSE;
	m_hUsbManagerMod = NULL;
	m_hUIMod = NULL;
}

DllWrapper::~DllWrapper(void)
{
	if (NULL != m_hUsbManagerMod)
	{
		FreeLibrary(m_hUsbManagerMod);
	}
	if (NULL != m_hUIMod)
	{
		FreeLibrary(m_hUIMod);
	}
}

BOOL DllWrapper::Init()
{
	if (m_isInited)
	{
		return TRUE;
	}
	if (NULL == m_hUsbManagerMod)
	{
		m_hUsbManagerMod = LoadLibrary(TAX_USB_MANAGER_DLL_NAME);
		if (NULL == m_hUsbManagerMod)
		{
			//OutputDebugStringA("LoadLibrary failed\n");
			return FALSE;
		}
	}		
	pCreateAppConfigObject = (PFNCreateAppConfigObject)GetProcAddress(m_hUsbManagerMod, "CreateAppConfigObject");
	if (NULL == pCreateAppConfigObject)
	{
		//OutputDebugStringA("LoadLibrary failed\n");
		return FALSE;
	}		

	pInitAppRegisterInfo = (PFNInitAppRegisterInfo)GetProcAddress(m_hUsbManagerMod, "InitAppRegisterInfo");
	if (NULL == pInitAppRegisterInfo)
	{
		return FALSE;
	}
	pCreateRegisterAppInfoObject = (PFNCreateRegisterAppInfoObject)GetProcAddress(m_hUsbManagerMod, "CreateRegisterAppInfoObject");
	if (NULL == pCreateRegisterAppInfoObject)
	{
		return FALSE;
	}

	pGetAppRegisterInfoByExePath = (PFNGetAppRegisterInfoByExePath)GetProcAddress(m_hUsbManagerMod, "GetAppRegisterInfoByExePath");
	if (NULL == pGetAppRegisterInfoByExePath)
	{
		return FALSE;
	}

	pGetAppRegisterInfoByinstallPath = (PFNGetAppRegisterInfoByinstallPath)GetProcAddress(m_hUsbManagerMod, "GetAppRegisterInfoByinstallPath");
	if (NULL == pGetAppRegisterInfoByinstallPath)
	{
		return FALSE;
	}

	pGetConfigBySerialNum = (PFNGetConfigBySerialNum)GetProcAddress(m_hUsbManagerMod, "GetConfigBySerialNum");
	if (NULL == pGetConfigBySerialNum)
	{
		return FALSE;
	}

	pIsOtherInstanceRunning = (PFNIsOtherInstanceRunning)GetProcAddress(m_hUsbManagerMod, "IsOtherInstanceRunning");
	if (NULL == pIsOtherInstanceRunning)
	{
		return FALSE;
	}

	pGetTaxUsbManager = (PFNGetTaxUsbManager)GetProcAddress(m_hUsbManagerMod, "GetTaxUsbManager");
	if (NULL == pGetTaxUsbManager)
	{
		return FALSE;
	}

	if (NULL == m_hUIMod)
	{
		m_hUIMod = LoadLibrary(TAX_BACKUP_UI_DLL_NAME);
		if (NULL == m_hUIMod)
		{
			return FALSE;
		}
	}

	pGetPassword = (PFNGetPassword)GetProcAddress(m_hUIMod, "GetPassword");
	if (NULL == pGetPassword)
	{
		return FALSE;
	}

	pShowProgress = (PFNShowProgress)GetProcAddress(m_hUIMod, "ShowProgress");
	if (NULL == pShowProgress)
	{
		return FALSE;
	}

	pHideProgress = (PFNHideProgress)GetProcAddress(m_hUIMod, "HideProgress");
	if (NULL == pHideProgress)
	{
		return FALSE;
	}

	m_isInited = TRUE;

	return TRUE;
}

void DllWrapper::Release()
{
	if (NULL != m_hUsbManagerMod)
	{
		FreeLibrary(m_hUsbManagerMod);
		m_hUsbManagerMod = NULL;
	}
	if (NULL != m_hUIMod)
	{
		FreeLibrary(m_hUIMod);
		m_hUIMod = NULL;
	}
}